package com.estate.house.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.estate.bean.Building;
import com.estate.house.feign.QuartersFeign;
import com.estate.house.service.BuildingService;
import com.estate.mapper.BuildingMapper;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements BuildingService {

    @Resource
    private QuartersFeign quartersFeign;

    @Override
    public JsonResult findById(String id) {
        Building building = this.getById(id);
        return ResultTool.success(building);
    }

    @Override
    public JsonResult listByPage(int page, int size) {
        PageHelper.startPage(page, size);
        List<Building> list = this.list();
        list.forEach(e -> {
            e.setQuarters(quartersFeign.find(e.getCommunityId()));
        });
        PageInfo<Building> pageInfo = new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }

    @Override
    public JsonResult addBuilding(Building building) {
        boolean b = this.save(building);
        if (!b) {
            return ResultTool.fail(501, "添加栋数失败");
        }
        return ResultTool.success("添加成功");
    }

    @Override
    public JsonResult updateBuilding(Building building) {
        boolean b = this.updateById(building);
        if (!b) {
            return ResultTool.fail(503, "修改栋数失败");
        }
        return ResultTool.success("修改成功");
    }

    @Override
    public JsonResult deleteBuilding(String id) {
        boolean b = this.removeById(id);
        if (!b) {
            return ResultTool.fail(502, "删除栋数失败");
        }
        return ResultTool.success("删除成功");
    }
}
